﻿-- =============================================
-- Author:  <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ChangeUserDetailsByEmail]
 @Email nvarchar(100),
 @Handphone nvarchar(15)='' ,
 @Name nvarchar(100)='',
 @Password nvarchar(300)='',
 @ResultType nvarchar(30) OUTPUT,
 @ResultMessage nvarchar(250) OUTPUT
AS
BEGIN TRANSACTION

 SET @Email = LTRIM(RTRIM(@Email))
 SET @Handphone = LTRIM(RTRIM(@Handphone))
 SET @Name = LTRIM(RTRIM(@Name))
 SET @Password = LTRIM(RTRIM(@Password))
 SET @ResultMessage = ''
 DECLARE @ChangeFlag INT
 SET @ChangeFlag = 0
 
    IF  EXISTS(SELECT * FROM Mobile_User WHERE Email = @Email)
  BEGIN
        --This environmentName does not exists, so insert...
        --INSERT INTO Mobile_User (Email,Handphone,Name,Password)
        --values (@Email,@Handphone,@Name,@Password)
        SET @ResultMessage = 'NO PARAMTER UPDATED.'
        IF @Password !='' -- UPDATE PASSWORD BY EMAIL
   BEGIN
    UPDATE Mobile_User SET Password = @Password WHERE Email = @Email
    SET @ChangeFlag = 1
   END
        IF @Handphone !='' 
   BEGIN
    UPDATE Mobile_User SET Handphone = @Handphone WHERE Email = @Email
    SET @ChangeFlag = @ChangeFlag + 1
   END  
        IF @Name !='' -- UPDATE PASSWORD BY EMAIL
   BEGIN
    UPDATE Mobile_User SET Name = @Name WHERE Email = @Email
    SET @ChangeFlag = @ChangeFlag + 1
   END 
   
            -- I get an error if I decomment the 2 lines underneath :-(
        SET @ResultType = 'Success'
        IF @ChangeFlag > 0
   SET @ResultMessage = CAST(@ChangeFlag as varchar(2)) + ' PARAMTER UPDATED.'
  END
 ELSE
  BEGIN
        --This environmentName does not exists so throw error

        SET @ResultType = 'Error'
        SET @ResultMessage = 'Email does not exist'
  END
COMMIT  
    SELECT @ResultType as N'@ResultType',
  @ResultMessage as N'@ResultMessage'
